package org.brownflat.watergate.dao;

import org.brownflat.utils.util.SecretEncoder;
import org.brownflat.watergate.domain.User;
import org.hibernate.Criteria;
import org.springframework.stereotype.Repository;

import static org.hibernate.criterion.Restrictions.eq;

/**
 * <p>&copy Rupert Jones 2011,2012</p>
 *
 * @author rup
 */
@Repository
public class UserDAO extends AbstractHibernateDAO<User> {

    public UserDAO() {
        super(User.class);
    }

    public User login(String username, String password) {
        String hash = SecretEncoder.hash(password);
        Criteria criteria = getCriteria().add(eq("username", username)).add(eq("password", hash));
        return (User) criteria.uniqueResult();
    }

    public User findByUsername(String username) {
        Criteria criteria = getCriteria().add(eq("username", username));
        return (User) criteria.uniqueResult();
    }

    public User authenticate(String username, String password) {
        return login(username, password);
    }
}
